package Common
{
	import Base.Net.NetSocket;
	
	import Common.ActionManager;
	import Common.GameAction;
	import Common.GameDefine;
	import Common.GameNetEvent;
	import Common.GameNetManager;
	import Common.MapDirection;
	import Common.RenderManager;
	
	import flash.display.BitmapData;
	
	import flashx.textLayout.formats.Direction;
	
	/**
	 * ...
	 * @author ...
	 */
	public class GameCreatureAction 
	{
		public var textureID:int = GameDefine.INVALIDID;
		public var id:int = 0;
		public var action:int = GameAction.STAND;
		public var index:int = 0;
		public var frame:int = 0;
		public var isOver:Boolean = false;
		
		public var direct:int = MapDirection.SOUTHWEST;
		
		public var delay:Number = GameAction.ACTIONDELAY;
		public var timeDelay:Number = 0;
		
		public var actionData:Object = null;
		public var actionObject:Object = null;
		
		public var fun:Function = null;
		
		public var object:Object = null;
		
		
		public function GameCreatureAction()
		{
			
		}
		
		
		public function init( obj:Object = null ):void
		{
			object = obj;
		}
		
		
		public function release():void
		{
			
		}
		
		
		public function setID( id:int ):void
		{
			this.id = id;
			ActionManager.instance.load( id , onLoad );
			frame = 0;
		}
		
		
		public function onLoad( obj:Object ):void
		{
			actionData = obj;
			
			setAction( action , direct , fun );
		}
		
		
		public function setAction( a:int , dir:int , f:Function = null ):void
		{
			fun = f;
			direct = dir;
			action = a;
			
			frame = 0;
			isOver = false;
			
			if ( actionData )
			{
				actionObject = actionData.action[ direct ][ action ];
				delay = actionObject.time / actionObject.frame * 0.8;
			}
			else
			{
				delay = GameAction.ACTIONDELAY;
				
				if ( fun != null )
				{
					fun( object );
				}
			}
			
			ActionChange();
		}
		
		
		public function update( delay:Number ):void
		{
			timeDelay += delay;
			
			if ( timeDelay > this.delay ) 
			{
				timeDelay = 0;
				
				if ( isOver )
				{
					return;
				}
				
				ActionChange();
			}
		}
		
		
		public function ActionChange():void
		{
			if ( !actionObject )
			{
				textureID = GameDefine.INVALIDID;
				return;
			}
			
			RenderManager.redraw = true;
			
			textureID = actionObject.action[ frame ];
			
			loadBmp( textureID );
			
			if ( action == GameAction.ATTACK ||action == GameAction.BOW ||action == GameAction.DEFENCE ||
				action == GameAction.FALL || action == GameAction.HURT || action == GameAction.SIT ) 
			{
				if ( frame + 1 == actionObject.frame )
				{
					isOver = true;
					
					if ( fun != null )
					{
						fun( object );
					}
					
					return;
				}
			}
			else
			{
				if ( frame + 1 == actionObject.frame )
				{
					frame = 0;
					return;
				}
			}
			
			frame++;
		}
		
		
		public function loadBmp( id:int ):void
		{
			// 能优化？
			if ( ActionManager.instance.bmpData[ id ] )
			{
				return;
			}
			
			var obj:Object = actionData.image[ id ];
			
			var bd:BitmapData = new BitmapData( obj.w , obj.h );
			
			var count:int = 0;
			for( var i:int = 0 ; i < obj.h ; i++ )
			{
				for ( var j:int = 0 ; j < obj.w ; j++ )
				{
					var c:int = ActionManager.instance.pal[ obj.data[ count++ ] ];
					
					bd.setPixel32( j , i , c );
				}
			}
			
			obj.bd = bd;
			
			ActionManager.instance.bmpData[ id ] = obj;
		}
		
		
		public function sendActionMsg( id:int ):void
		{
//			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;
//			
//			netSocket.writeShort( GameNetEvent.SEND_ACTION_SIZE );
//			netSocket.writeShort( GameNetEvent.SEND_ACTION_ID );
//			
//			netSocket.writeInt( owner.ID );
//			netSocket.writeInt( id );
//			
//			netSocket.Send();
		}
		
	}
	
}